package com.hopu.v1.service.admin.question.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.v1.mapper.admin.question.QuestionsMapper;
import com.hopu.v1.pojo.common.PageData;
import com.hopu.v1.pojo.question.Questions;
import com.hopu.v1.pojo.question.QuestionsOptions;
import com.hopu.v1.service.admin.question.QuestionsOptionsService;
import com.hopu.v1.service.admin.question.QuestionsService;
import com.hopu.v1.vo.QuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author YwaiX
 * @description 针对表【t_questions(题目表)】的数据库操作Service实现
 * @createDate 2024-09-26 15:20:06
 */
@Service
public class QuestionsServiceImpl extends ServiceImpl<QuestionsMapper, Questions>
		implements QuestionsService {
	
	@Autowired
	private QuestionsMapper questionsMapper;
	
	@Autowired
	private QuestionsOptionsService questionsOptionsService;
	
	@Override
	public PageData<Questions> getQuestionsList(Integer page, Integer size, Integer questionBanksId, String questionTitle, String questionType, Integer status) {
		List<Questions> questionsList = questionsMapper.getQuestionsList((page - 1) * size, size, questionBanksId, questionTitle, questionType, status);
		Integer total = questionsMapper.getTotal(questionBanksId, questionTitle, questionType, status);
		return new PageData<>(page, size, total, questionsList);
	}
	
	@Transactional
	@Override
	public void insert(QuestionVo questionVo) {
		Questions questions = new Questions();
		questions.setQuestionBanksId(questionVo.getQuestionBanksId());
		questions.setQuestionTitle(questionVo.getQuestionTitle());
		questions.setAnalysis(questionVo.getAnalysis());
		questions.setQuestionType(questionVo.getQuestionType());
		questions.setSort(questionVo.getSort());
		questions.setStaffId(questionVo.getStaffId());
		questions.setStudentId(0);
		questions.setStatus(questionVo.getQuestionStatus());
		questions.setCreateTime(LocalDateTime.now());
		
		baseMapper.insert(questions);
		
		List<QuestionsOptions> options = questionVo.getOptions();
		
		for (QuestionsOptions option : options) {
			option.setQuestionId(questions.getId());
			option.setCreateTime(LocalDateTime.now());
		}
		
		questionsOptionsService.insert(options);
	}
	
	@Override
	public QuestionVo getQuestionById(Integer id) {
		return questionsMapper.getQuestionById(id);
	}
	
	@Transactional
	@Override
	public void updateQuestionById(QuestionVo questionVo) {
		Questions questions = new Questions();
		questions.setId(questionVo.getId());
		questions.setQuestionBanksId(questionVo.getQuestionBanksId());
		questions.setQuestionTitle(questionVo.getQuestionTitle());
		questions.setAnalysis(questionVo.getAnalysis());
		questions.setQuestionType(questionVo.getQuestionType());
		questions.setSort(questionVo.getSort());
		questions.setStaffId(questionVo.getStaffId());
		questions.setStatus(questionVo.getQuestionStatus());
		questions.setUpdateTime(LocalDateTime.now());
		
		List<QuestionsOptions> options = questionVo.getOptions();
		List<Integer> optionIds = new ArrayList<>();
		
		for (QuestionsOptions option : options) {
			optionIds.add(Math.toIntExact(option.getId()));
			option.setUpdateTime(LocalDateTime.now());
		}
		
		
		questionsOptionsService.deleteByIds(optionIds);
		
		baseMapper.deleteById(questions);
		
		baseMapper.insert(questions);
		
		questionsOptionsService.insert(options);
	}
	
	@Transactional
	@Override
	public void deleteQuestionByIds(List<Integer> questionIds) {
		List<Integer> optionIds = questionsMapper.selectOptionsById(questionIds);
		questionsOptionsService.deleteByIds(optionIds);
		
		baseMapper.deleteByIds(questionIds);
	}
}




